package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.impl.CategoryServiceImpl;
import com.itheima.reggie.service.impl.DishFlavorServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.management.Query;
import java.util.*;

@RestController
//请求地址
@RequestMapping("/dish")
public class DishController {
    @Autowired
    DishService dishservice;
    @Autowired
    CategoryService categoryService;
    @Autowired
    DishFlavorService dishFlavorService;

    //菜品管理添加菜品,口味到数据库方法
    @PostMapping
    public R addDish(@RequestBody DishDto dishDto) {
        dishservice.saveDish(dishDto);
        return R.success("添加成功");
    }

    //菜品管理分页展示
    @GetMapping("/page")
    public R pageQuery(int page,int pageSize,String name){
        // 创建page对象，设置当前页和每页大小
        IPage<Dish> pageInfo = new Page<>(page,pageSize);
        // 封装查询条件 where name like '%%'
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.like(StringUtils.isNotEmpty(name),Dish::getName,name);
        //封装排序
        qw.orderByDesc(Dish::getUpdateTime);
        //开始查询
        dishservice.page(pageInfo,qw);
        long total = pageInfo.getTotal();//总条数
        List<Dish> records = pageInfo.getRecords();// 查询列表数据
        // records: List<Dish>
        // 只有菜品分类id：categoryId，没有分类名称
        // 根据菜品分类id，查询菜品分类数据。在set进去
        List<DishDto> dishDtos = new ArrayList<>();
        for (Dish dish : records) {
            //获取菜品分类id
            Long categoryId = dish.getCategoryId();
            Category category = categoryService.getById(categoryId);
            //获取到了当前分类的名称
            String categoryName = category.getName();
            // 新建一个DishDto 将分类名称set进入
            DishDto dishDto = new DishDto();
            dishDto.setCategoryName(categoryName);
            // 还需要将Dish dish 数据放入到 DishDto dishDto 里面
            BeanUtils.copyProperties(dish,dishDto);
            dishDtos.add(dishDto);
        }
        Map map = new HashMap();
        map.put("total",total);
        map.put("records",dishDtos);
        return R.success(map);
    }
    //回显修改数据
    @GetMapping("/{id}")
    public R selectById (@PathVariable Long id){
        /*Dish dish = dishservice.getById(id);
        Long categoryId = dish.getCategoryId();
        Category category = categoryService.getById(categoryId);
        DishDto dishDto=new DishDto();
        BeanUtils.copyProperties(category,dishDto);*/
        DishDto dishDto= dishservice.selectDish(id);
        return R.success(dishDto);
    }
    //修改菜品
    @PutMapping
    public R updateCai(@RequestBody DishDto dishDto){
        dishservice.updateCai(dishDto);
       return R.success("修改成功");
    }
    //删除菜品
    @DeleteMapping
    public R deleteCai(@RequestParam List<Long>ids){
        dishservice.delectCai(ids);
        return R.success("删除成功");
    }
    //起售停售
    @PostMapping("status/{status}")
    public R sellAndNotSell(@PathVariable Integer status,@RequestParam List<Long> ids){
       dishservice.sellAndNotSell(status,ids);
        return R.success("操作成功");
    }


    //添加套餐时的方法 因为客户端需要选择口味规格所以用dishDto包装
    @GetMapping("/list")
    public R categoryList(Dish dish){
        //1.先用dish拿到所有菜品 根据套餐id进行where条件过滤
        QueryWrapper<Dish> qw=new QueryWrapper<>();
        qw.lambda().eq(Dish::getCategoryId,dish.getCategoryId());
        //list<dish>改为list<dishDto>
        List<Dish> list = dishservice.list(qw);
        //创建dishDto对象集合 并遍历菜品集合把每个菜品拷贝进去 口味set进去
        List<DishDto>dishDtos=new ArrayList<>();
        for (Dish dish1 : list) {
            //新建dto对象容器
            DishDto dishDto=new DishDto();
            //拷贝菜品进容器
            BeanUtils.copyProperties(dish1,dishDto);
            //通过口味service拿到口味集合 根基菜品id进行过滤
            QueryWrapper<DishFlavor> qw1=new QueryWrapper<>();
            qw1.lambda().eq(DishFlavor::getDishId,dish1.getId());
            List<DishFlavor> dishFlavors = dishFlavorService.list(qw1);
           dishDto.setFlavors(dishFlavors);
           dishDtos.add(dishDto);
        }
        return R.success(dishDtos);
    }
}
